package com.kalix.ksh.proint.biz;

import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;
import com.kalix.ksh.proint.api.biz.IRealtimeBeanService;
import com.kalix.ksh.proint.api.dao.IRealtimeBeanDao;
import com.kalix.ksh.proint.api.model.InfUtil;
import com.kalix.ksh.proint.entities.RealtimeBean;
import org.json.JSONArray;
import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Wanghw on 2018/10/10.
 * 设备运行状态请求
 */
public class RealtimeBeanServiceImpl extends GenericBizServiceImpl<IRealtimeBeanDao, RealtimeBean> implements IRealtimeBeanService {
    public RealtimeBeanServiceImpl(){
        super.init(RealtimeBean.class.getName());
    }
    public static String wsIp = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.ksh.portconf");
    public static String wsPort =(String)ConfigUtil.getConfigProp("FREEBOARD_PORT","config.ksh.portconf");

   /**
    *
    * 功能描述:根据设备运行状态需要数据格式返回数据
    * 第二张图左边，雷达图
    *
    * @auther: Whw
    * @date: 2019/1/14 0014 17:17
    * @param:
    * @return:
    */

    public String getRealValue() {
        String sql = "select ru from RealtimeBean ru";
        List<RealtimeBean> list = dao.find(sql);
        JSONArray str =new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            String uuid = list.get(i).getUuid();
            String name = list.get(i).getName();
            String url = "http://"+wsIp+":"+wsPort+"/metrics/Metrics.moPath=\"Windows.domain=\"defaultEngine\",uuid=\""+uuid+"\"\",name=\"OSCPU_CPU_Load\"?fresh=1860";
            double number = getData(url);
            String url2 = "http://"+wsIp+":"+wsPort+"/metrics/Metrics.moPath=\"Windows.domain=\"defaultEngine\",uuid=\""+uuid+"\"\",name=\"OSMEM_UsedRatio\"?fresh=1860";
            double number2 = getData(url2);
            String url3 = "http://"+wsIp+":"+wsPort+"/metrics/Metrics.moPath=\"Windows.domain=\"defaultEngine\",uuid=\""+uuid+"\"\",name=\"OSUptime_Uptime\"?fresh=1860";
            double num = getData(url3);
            DecimalFormat df   = new DecimalFormat("######0.00");
            double number3 = Double.parseDouble(df.format(num/60/24));
            JSONObject jobj = new JSONObject();
            JSONObject obj = new JSONObject();
            obj.put("name", name);
            obj.put("CPULoad", "CPU使用率:\r"+number+"%\r(T)");
            obj.put("OUsedRatio", "内存使用率:\r"+number2+"%\r(L)");
            obj.put("Uptime", "持续运营时间:\r"+number3+"天\r(R)");
            jobj.put("title",obj);
            JSONArray arr = new JSONArray();
            arr.put(number);
            arr.put(number2);
            arr.put(number3);
            JSONObject ob = new JSONObject();
            ob.put("value",arr);
            JSONArray arra = new JSONArray();
            arra.put(ob);
            jobj.put("value",arra);
            str.put(jobj);
        }
        String stri = str.toString();
        return stri;
    }
    /**
     *
     * 功能描述: 调用获取服务数据
     *
     * @auther: Whw
     * @date: 2019/1/14 0014 17:17
     * @param:
     * @return:
     */
    public  double getData(String url){
        String  data= InfUtil.interfaceUtil(url,"");
        JSONObject obj = new JSONObject(data);
        double min= obj.getJSONObject("value").getDouble("value");
        return min;
    }
}
